package com.qwer.service.accesscontrol.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.qwer.mapper.accesscontrol.AccControlMapRepository;
import com.qwer.model.accesscontrol.AccControlMapInfo;
import com.qwer.service.accesscontrol.AccControlMapService;

@Service
public class AccControlMapServiceImpl implements AccControlMapService{
    
    @Autowired
    private AccControlMapRepository repository;
    
    

    @Override
    public Page<AccControlMapInfo> findList(Pageable pageable,AccControlMapInfo model) {
        
        Specification<AccControlMapInfo> spec = new Specification<AccControlMapInfo>() {

            @Override
            public Predicate toPredicate(Root<AccControlMapInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                
                List<Predicate> list = new ArrayList<>();
                /*if (StringUtils.isNotEmpty(model.getApiName())) {
                    list.add(cb.like(root.get("apiName"),"%"+model.getApiName()+"%"));
                    
                }
                list.add(cb.equal(root.get("userId"), "1"));*/
                
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
            
        };   
        Page<AccControlMapInfo> orderMasterPage = repository.findAll(spec,pageable);
        
        List<AccControlMapInfo> orderDTOList = orderMasterPage.getContent();

        return new PageImpl<AccControlMapInfo>(orderDTOList, pageable, orderMasterPage.getTotalElements());
    }


    @Override
    public void save(AccControlMapInfo model) {
         repository.save(model);
    }

    @Override
    public void deleteById(Integer id) {
        
         repository.delete(id);;
    }


    @Override
    public AccControlMapInfo findById(Integer id) {
        return repository.findOne(id);
    }


    @Override
    public List<AccControlMapInfo> getUserCheckDatas(Integer userId, Integer callId,String type) {
        
        return repository.findByUserIdAndCallIdAndType(userId, callId,type);
    }


    @Override
    public List<AccControlMapInfo> findByCallUserIdAndCallIdAndType(AccControlMapInfo model) {
        // TODO Auto-generated method stub
        
        return repository.findByCallUserIdAndCallIdAndType(model.getCallUserId(), model.getCallId(), model.getType());
    }


	@Override
	public void deleteByCallIdAndUserId(Integer callId, Integer userId) {
		repository.removeByCallIdAndUserId(callId, userId);
		
	}

}
